package com.itboye.pondteam.utils;

import android.util.Log;

import com.itboye.pondteam.bean.WeekModel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import static com.itboye.pondteam.volley.TimesUtils.getWeeksBinary;
import static com.itboye.pondteam.volley.TimesUtils.parseTime;
import static com.itboye.pondteam.volley.TimesUtils.utc2Local;

public class NextTime {

    static SimpleDateFormat simpleTime = new SimpleDateFormat("yyyyMMddHHmm");
    static SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMdd");
    static SimpleDateFormat simpleWeek = new SimpleDateFormat("EEEE");
    static SimpleDateFormat simpleHHmm = new SimpleDateFormat("HHmm");

    public static String nextTime(int weekBinary, String currentWeek, String hour) {
        String weeks = getWeeksBinary(weekBinary);
        String weekBinaryString[] = weeks.split(",");
        String yyyyMMdd = simpleDate.format(System.currentTimeMillis());
        ArrayList<WeekModel> weekModelArrayList = new ArrayList<>();
        String device_cl_tm = utc2Local(hour, simpleHHmm.toPattern(), simpleHHmm.toPattern());//获取设备清洗的本地时间
        String current_tm = simpleHHmm.format(System.currentTimeMillis());
        boolean isFind = false;
        for (int i = 0; i < 7; i++) {
            int intValue = Integer.parseInt(yyyyMMdd);
            String week = parseTime(yyyyMMdd, simpleDate.toPattern(), simpleWeek.toPattern(), true);
            System.out.println(intValue + "-<>---外层进入" + week);
            for (String s : weekBinaryString) {
                WeekModel weekModel = new WeekModel();
                //s格式仅为英文状态下的星期
                weekModel.setContain(false);
                if (s.contains(week)) {
                    System.out.println(intValue + "-<>---进入" + s);
                    weekModel.setContain(true);
                    weekModel.setTime(intValue + "");
                    weekModel.setDate(intValue + "" + device_cl_tm);
                    weekModel.setWeek(s);
//                    isFind = true;
                    weekModelArrayList.add(weekModel);
                }

            }
//            if (isFind) {
//                break;
//            }
            intValue++;
            yyyyMMdd = intValue + "";
        }

        for (WeekModel weekModel : weekModelArrayList) {
            long cleanDate = Long.parseLong(weekModel.getDate());
            long todayDate = Long.parseLong(simpleTime.format(System.currentTimeMillis()));
            if (cleanDate > todayDate) {
                yyyyMMdd = weekModel.getTime();
                break;
            }
//            if (weekModel.isContain()) {
//                if (((Integer.valueOf(device_cl_tm) < Integer.valueOf(current_tm)))) {
//                    continue;
//                }
//                yyyyMMdd = weekModel.getDate();
//
//                break;
//            }
        }
        System.out.println(device_cl_tm + "-<>-设备清洗本地时间--" + current_tm + "  当前时间");
        System.out.println(weeks + "-<>---" + yyyyMMdd + "  集合：" + weekModelArrayList.size());
        //1、先获取设备设置过来的星期
        //2、获取当前的星期，并根据当前的日期计算7天之内的日期（集合内 存储星期，日期参数，isSetDate）
        //3、根据2步骤获取到集合中的星期进行遍历，如果与相等，则将设备所设置的星期转换为对应的日期
        //4、遍历3步骤后的集合，取出最近的一个isSetDate==true的日期
        //输出格式：yyyy-MM-dd HH:mm
        String time = parseTime(yyyyMMdd + device_cl_tm, simpleDate.toPattern() + simpleHHmm.toPattern(), "yyyy-MM-dd HH:mm");
        System.out.println("-<>---下次清洗时间" + time);
        return time;
    }

    /**
     * 转换到24小时
     *
     * @param ampm
     * @return
     */
    public static String convert(String ampm) {
//        LogUtils.v("next_time", "原有字符串：" + ampm);
//        final SimpleDateFormat format = new SimpleDateFormat("a hh:mm", Locale.ENGLISH);
//        SimpleDateFormat parse = null;
//        parse = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
//        try {
//            return format.format(parse.parse(ampm));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        return ampm;
//        return "00:00";
    }

    /**
     * 转换到为am pm模式显示
     *
     * @param ampm
     * @return
     */
    public static String convert(String ampm,String inputFormat,String outPutFormat) {
//        LogUtils.v("next_time", "原有字符串：" + ampm);
//        final SimpleDateFormat format = new SimpleDateFormat(outPutFormat, Locale.ENGLISH);
//        SimpleDateFormat parse = null;
//        parse = new SimpleDateFormat(inputFormat, Locale.ENGLISH);
//        try {
//            return format.format(parse.parse(ampm));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        return ampm;
//        return "00:00:00";
    }
}
